﻿using JV.Com.Business;
using JV.Com.Business.Log;
using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.ApplicationModel.Search;

namespace JV.Com.Internals
{
    public abstract class PageBaseViewModel : ViewModel, IHasMainNavigationService
    {
        public event EventHandler DataContextLoaded;

        protected readonly INavigationService m_navigationService;

        public PageBaseViewModel(INavigationService navigationService)
        {
            m_navigationService = navigationService;
        }

        public PageBaseViewModel() { }

        protected void RaiseDataContextLoaded()
        {
            if(DataContextLoaded != null) DataContextLoaded(this, EventArgs.Empty);
        }

        public INavigationService NavigationService { get { return m_navigationService; } }
        public virtual ICommand GoBackCommand { get { return new DelegateCommand(() => m_navigationService.GoBack(), () => m_navigationService.CanGoBack()); } }

        public abstract string PageTitle { get; }
        public void OnSearchQuerySubmitted(string query)
        {
            if (string.IsNullOrEmpty(query)) return;
            m_navigationService.Navigate("SearchResult", query);
        }

        private string m_query;
        public string Query
        {
            get { return m_query; }
            set { SetProperty<string>(ref m_query, value); }
        }

        private bool m_inLoading;
        public bool InLoading
        {
            get { return m_inLoading; }
            set { SetProperty<bool>(ref m_inLoading, value); }
        }

        public async Task OnSearchSuggestionsRequested(string query, SearchSuggestionsRequest request)
        {
            if (string.IsNullOrEmpty(query)) return;

            var defer = request.GetDeferral();
            
            try
            {
                var suggestions = await Service.GetSuggestions(query);

                if (!request.IsCanceled && suggestions.Any())
                {
                    var sugNumber = 0;
                    foreach(var sug in suggestions)
                    {
                        sugNumber++;

                        if(sugNumber > 5)
                        {
                            break;
                        }

                        request.SearchSuggestionCollection.AppendQuerySuggestion(sug);
                    }
                }
            }
            catch(Exception ex)
            {
                MetroEventSource.Log.Error(string.Format("Error while getting suggestions from '{0}'.\n{1}", query, ex.ToString()));
            }
            finally
            {
                defer.Complete();
            }
        }
    }
}
